package com.lxd.sunso.product.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.lxd.sunso.common.utils.DateUnit;
import com.lxd.sunso.common.utils.PageUtils;
import com.lxd.sunso.common.utils.R;
import com.lxd.sunso.product.vo.TemplateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lxd.sunso.product.entity.TemplateEntity;
import com.lxd.sunso.product.service.TemplateService;


/**
 * @author lxd
 * @email sunlightcs@gmail.com
 * @date 2020-12-30 18:06:30
 */
@RestController
@RequestMapping("product/template")
public class TemplateController {
    @Autowired
    private TemplateService templateService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:template:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = templateService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:template:info")
    public R info(@PathVariable("id") Long id) {
        TemplateEntity template = templateService.getById(id);

        return R.ok().put("template", template);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:template:save")
    public R save(@RequestBody TemplateEntity template) {
        template.setCreatetime(DateUnit.dateFormat(new Date()));
        templateService.save(template);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:template:update")
    public R update(@RequestBody TemplateEntity template) {
        templateService.updateById(template);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:template:delete")
    public R delete(@RequestBody Long[] ids) {
        templateService.removetemplate(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 获取所有的数据模板名称和ID
     */
    @RequestMapping("/getTemNameAndId")
    public R getTemNameAndId() {
        List<TemplateVo> list = templateService.getTemNameAndId();
        return R.ok().put("list", list);
    }
}
